package io.renren.modules.sp.controller.front;

import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sp.dto.*;
import io.renren.modules.sp.entity.SpMatchEntity;
import io.renren.modules.sp.entity.SpMatchItemEntity;
import io.renren.modules.sp.service.SpMatchItemService;
import io.renren.modules.sp.service.SpMatchItemTeamService;
import io.renren.modules.sp.service.SpMatchService;
import io.renren.modules.sp.service.SpTeamUserService;
import io.renren.modules.sys.dao.SysCommDao;
import io.renren.modules.sys.dao.SysParamsDao;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
*
*
* @author Mark sunlightcs@gmail.com
* @since 3.0 2023-04-20
*/
@RestController
@RequestMapping("front/spmatch")
@Api(tags="领队成员")
public class FrontMatchController extends FrontBaseController{

    @Autowired
    private SysCommDao commDao;
    @Autowired
    private SpMatchItemService spMatchItemService;

    @Autowired
    private SpMatchService spMatchService;

    @Autowired
    private SysParamsDao sysParamsDao;

    @GetMapping("{id}")
    public Result<List<SpMatchItemDTO>> update(@PathVariable("id") Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("matchId", id);
        List<SpMatchItemDTO> list = spMatchItemService.list(params);
        return new Result<List<SpMatchItemDTO>>().ok(list);
    }

    @GetMapping("matchItem/{id}")
    public Result<SpMatchItemEntity> matchItem(@PathVariable("id") Long id, HttpServletRequest request) {
        SpMatchItemEntity entity = spMatchItemService.selectById(id);
        return new Result<SpMatchItemEntity>().ok(entity);
    }


    @GetMapping("info/{id}")
    public Result<SpMatchDTO> info(@PathVariable("id") Long id) {
        Map<String, Object> params = new HashMap<>();
        params.put("matchId", id);
        SpMatchEntity matchEntity = spMatchService.selectById(id);
        //entity 转 dto
        SpMatchDTO spMatchDTO = ConvertUtils.sourceToTarget(matchEntity, SpMatchDTO.class);
        //把beginDate和endDate转成字符串
        String matchDate = DateUtils.format(matchEntity.getBeginDate(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);
        matchDate += "~" + DateUtils.format(matchEntity.getEndDate(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);
        spMatchDTO.setMatchDate(matchDate);
        //组合报名时间
        String signupDate = DateUtils.format(matchEntity.getBeginSignup(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);
        signupDate += "~" + DateUtils.format(matchEntity.getEndSignup(), DateUtils.DATE_FORMAT_MONTH_DAY_ZH);
        spMatchDTO.setSignupDate(signupDate);
        return new Result<SpMatchDTO>().ok(spMatchDTO);
    }

    // 查询全局配置
    @GetMapping("sysParams")
    public Result<Map<String, Object>> sysParams() {
        Map<String, Object> params = new HashMap<>();
        //查询报名表重传次数
        String reUploadTimes = sysParamsDao.getValueByCode("reUploadTimes");
        params.put("reUploadTimes", reUploadTimes);
        //查询是否允许重传
        String isReUploadFile = sysParamsDao.getValueByCode("isReUploadFile");
        params.put("isReUploadFile", isReUploadFile);
        //查询是否允许晋级
        String isRise = sysParamsDao.getValueByCode("stopRise");
        params.put("stopRise", isRise);
        return new Result<Map<String, Object>>().ok(params);
    }

}
